﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model._Admin;
using HengTong.Model.Db.Other;


namespace HengTong.ViewModel.Visitor.SysVisitorInformationVms
{
    public partial class
        SysVisitorInformationListVm : BasePagedListVM<SysVisitorInformationView, SysVisitorInformationSearcher>
    {
        protected override IEnumerable<IGridColumn<SysVisitorInformationView>> InitGridHeader()
        {
            return new List<GridColumn<SysVisitorInformationView>>
            {
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Phone),
                this.MakeGridHeader(x => x.IdCardNo),
                this.MakeGridHeader(x => x.IdCardFrontPhotoId).SetFormat(IdCardFrontPhotoIdFormat),
                this.MakeGridHeader(x => x.IdCardBackPhotoId).SetFormat(IdCardBackPhotoIdFormat),
                this.MakeGridHeader(x => x.Company),
                this.MakeGridHeader(x => x.CarNo),
                this.MakeGridHeader(x => x.UserName),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.BlackStatus),
                this.MakeGridHeader(x => x.BlackReason),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        private List<ColumnFormatInfo> IdCardFrontPhotoIdFormat(SysVisitorInformationView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.IdCardFrontPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.IdCardFrontPhotoId, 640, 480),
            };
        }

        private List<ColumnFormatInfo> IdCardBackPhotoIdFormat(SysVisitorInformationView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.IdCardBackPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.IdCardBackPhotoId, 640, 480),
            };
        }


        public override IOrderedQueryable<SysVisitorInformationView> GetSearchQuery()
        {
            if (Wtm.LoginUserInfo == null)
            {
                MSD.AddModelError(" ", "请先登录");
                return null;
            }

            //判断当前登陆人是否是游客角色999
            bool isGuest = Wtm.LoginUserInfo.Roles.Any(e => e.RoleCode == "999");
            bool isNotAdmin = !Wtm.LoginUserInfo.Roles.Any(e => e.RoleCode == "001");
            if (Wtm.LoginUserInfo.Roles.Count == 0 || isGuest || isNotAdmin)
            {
                Searcher.UserId = Guid.Parse(Wtm.LoginUserInfo.UserId);
            }

            var query = DC.Set<SysVisitorInformation>()
                .CheckContain(Searcher.Name, x => x.Name)
                .CheckContain(Searcher.Phone, x => x.Phone)
                .CheckContain(Searcher.IdCardNo, x => x.IdCardNo)
                .CheckContain(Searcher.Company, x => x.Company)
                .CheckContain(Searcher.CarNo, x => x.CarNo)
                .CheckEqual(Searcher.UserId, x => x.UserId)
                .CheckEqual(Searcher.BlackStatus, x => x.BlackStatus)
                .Select(x => new SysVisitorInformationView
                {
                    ID = x.ID,
                    Name = x.Name,
                    Phone = x.Phone,
                    IdCardNo = x.IdCardNo,
                    IdCardFrontPhotoId = x.IdCardFrontPhotoId,
                    IdCardBackPhotoId = x.IdCardBackPhotoId,
                    Company = x.Company,
                    CarNo = x.CarNo,
                    UserName = x.User.Name,
                    Remark = x.Remark,
                    BlackStatus = x.BlackStatus,
                    BlackReason = x.BlackReason,
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysVisitorInformationView : SysVisitorInformation
    {
        [Display(Name = "_Admin.Name")] public String UserName { get; set; }
    }
}